package com.malou.client.ventas.cajainicial;

import java.util.Date;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.malou.client.MalouConstants;

public class CajaInicialForm extends FormPanel {

	private NumberField saldoinicial;
	private NumberField montoencaja;
	private FormButtonBinding formbuttonbinding;

	private Button guardar;
	private MalouConstants constants = GWT.create(MalouConstants.class);
	private FieldSet montoFielSet;
	

	private LayoutContainer ingresarLayout;

	public CajaInicialForm() {
		setHeading("Caja Inicial");
		setSize(220, 200);
		setPadding(3);

		setFrame(true);
		setLabelAlign(LabelAlign.LEFT);
		setButtonAlign(HorizontalAlignment.CENTER);
		formbuttonbinding = new FormButtonBinding(this);

		CajaIniciales.cif = this;
		CajaIniciales.checkSaldoInicial();

		montoFielSet = new FieldSet();
		montoFielSet.setLayout(new ColumnLayout());
		montoFielSet.setHeading(constants.montoencaja());
		
		montoFielSet.setBorders(true);

		ingresarLayout = new LayoutContainer();
		ingresarLayout.setLayout(new ColumnLayout());
		ingresarLayout.setStyleAttribute("padding", "8px");
		ingresarLayout.setBorders(true);

		montoencaja = new NumberField();
		montoencaja.setStyleAttribute("marginLeft", "40px");
		montoencaja.setReadOnly(true);
		montoencaja.setInputStyleAttribute("textAlign", "center");
		montoencaja.setHideLabel(true);
		

		saldoinicial = new NumberField();
		saldoinicial.setAllowNegative(false);
		saldoinicial.setMaxValue(100000);
		saldoinicial.setAllowBlank(false);
		saldoinicial.setFieldLabel(constants.addmonto());
		saldoinicial.addKeyListener(new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				Integer c = event.getKeyCode();
				if (c==KeyCodes.KEY_ENTER ){
					save();
					
				}
	
			}

		});

		montoFielSet.add(setLayout(montoencaja, "60%", 0), new ColumnData(1));
		ingresarLayout.add(setLayout(saldoinicial, "90%", 80),
				new ColumnData(1));

		guardar = new Button(constants.guardar());
		guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				if (saldoinicial.isValid()) {
				save();

				}
			}

		});
		formbuttonbinding.addButton(guardar);
		addButton(guardar);
		add(montoFielSet);
		add(ingresarLayout);

	}

	private LayoutContainer setLayout(Component comp, String porcentaje,
			Integer withlabel) {
		LayoutContainer layout = new LayoutContainer();
		FormLayout fl = new FormLayout(LabelAlign.LEFT);
		fl.setLabelWidth(withlabel);
		layout.setLayout(fl);
		layout.add(comp, new FormData(porcentaje));

		return layout;
	}

	public void setMontoCaja(Double montocaja) {
		montoencaja.setValue(montocaja);
	}
	
	public void reset(){
		saldoinicial.clear();
	}
	
	private void save(){
		CajaInicial balance = new CajaInicial();
		balance.setFecha(new Date());
		Double monto = Double.valueOf(saldoinicial.getValue().toString());
		balance.setSaldoinicial(monto);
		CajaIniciales.save(balance);
	}
}
